package mycoffee.order.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public final class PrefUtils {

	private static final String TAG = "PrefUtils";

	public static boolean contains(Context con, String key) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : contains : key = " + key);
			return pref(con).contains(key);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return false;
		}
	}

	public static String getString(Context con, String key, String defValue) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : getString : key = " + key);
			return pref(con).getString(key, defValue);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return defValue;
		}
	}

	public static boolean getBoolean(Context con, String key, boolean defValue) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : getBoolean : key = " + key);
			return pref(con).getBoolean(key, defValue);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return defValue;
		}
	}

	public static int getInt(Context con, String key, int defValue) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : getInt : key = " + key);
			return pref(con).getInt(key, defValue);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return defValue;
		}
	}

	public static long getLong(Context con, String key, long defValue) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : getLong : key = " + key);
			return pref(con).getLong(key, defValue);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return defValue;
		}
	}

	public static float getFloat(Context con, String key, float defValue) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : getFloat : key = " + key);
			return pref(con).getFloat(key, defValue);
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
			return defValue;
		}
	}

	public static void writeString(Context con, String key, String value) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : writeString : key = " + key
					+ ", value = " + value);
			pref(con).edit().putString(key, value).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	public static void writeBoolean(Context con, String key, boolean value) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : writeBoolean : key = " + key
					+ ", value = " + value);
			pref(con).edit().putBoolean(key, value).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	public static void writeInt(Context con, String key, int value) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : writeInt : key = " + key
					+ ", value = " + value);
			pref(con).edit().putInt(key, value).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	public static void writeLong(Context con, String key, long value) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : writeLong : key = " + key
					+ ", value = " + value);
			pref(con).edit().putLong(key, value).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	public static void writeFloat(Context con, String key, float value) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : writeFloat : key = " + key
					+ ", value = " + value);
			pref(con).edit().putFloat(key, value).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	public static void remove(Context con, String key) {
		try {
			LogUtils.logInfo(TAG, "PrefUtils : remove : key = " + key);
			pref(con).edit().remove(key).commit();
		} catch (Exception e) {
			LogUtils.logError(TAG, e.toString());
		}
	}

	private static SharedPreferences pref(Context con) {
		return PreferenceManager.getDefaultSharedPreferences(con);
	}

}
